#include"LinkTree.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define true 1
#define false 0


struct student
{
    int id;
    char *name;
    int score;
};

//创建学生信息
struct student* CreateStudent(int id,char *name,int score)
{
    struct student* stu=(struct student*)malloc(sizeof(struct student));
    if(stu==NULL)
    {
        return NULL;
    }
    stu->id=id;
    stu->name=(char*)name;
    stu->score=score;
}


void FreeStudent(struct student *stu)
{
    if(stu==NULL)
    return;
    free(stu);
}


void Print(ElementType data)
{
    if(data.type==structPoint)//判断指针类型
    {
    struct student*stu=(struct student*)data.value;
    if(stu!=NULL)
    {
    printf("if : %d ,name : %s,score :%d\n",stu->id,stu->name,stu->score);
    }
    }
    else
    if(data.type==charPoint)//判断指针类型
    {
        char *temp=(char*)data.value;
        printf("%s\n",temp);
    }
}

int IsEqual(ElementType data1,ElementType data2)//判断两数据是否相等
{
    if(data1.type==structPoint&&data2.type==structPoint)
    {
    struct student *stu=(struct student*)data1.value;
    struct student *stu2=(struct student*)data2.value;
    if(stu->id== stu2->id)
    {
    return true;
    }
    return false;
    }
    else if(data1.type==charPoint&&data2.type==charPoint)
    {
        char *temp=(char*)data1.value;
        char *temp2=(char*)data2.value;
        if(strcmp(temp,temp2)==0)
        {
            return true;
        }
        return false;
    }
}
int main()
{
    LTree tree;
    InitLTree(&tree);//初始化
    struct UniversalType Typevalue;//创造结构体数据
    Typevalue.value="243class";
    Typevalue.type=charPoint;
    LTNode *node1=CreateTreeNode(Typevalue);

    Typevalue.value=CreateStudent(1,"zhangsan",100);
    Typevalue.type=structPoint;
    LTNode *node2=CreateTreeNode(Typevalue);

    Typevalue.value=CreateStudent(2,"lisi",90);
    Typevalue.type=structPoint;
    LTNode *node3=CreateTreeNode(Typevalue);
    // LTNode *node4=CreateTreeNode("shaojun");
    ConnectBranch(node1,node2);//连接
    ConnectBranch(node1,node3);
    //ConnectBranch(node1,node4);

    // LTNode *node5=CreateTreeNode("247class");
    // LTNode *node6=CreateTreeNode("liangtianchi");
    // LTNode *node7=CreateTreeNode("wanghan");
    // LTNode *node8=CreateTreeNode("fengencui");
    // ConnectBranch(node5,node6);
    // ConnectBranch(node5,node7);
    // ConnectBranch(node5,node8);

     ConnectBranch(tree.root,node1);
    // ConnectBranch(tree.root,node5);
    Travel(&tree,Print);

    Typevalue.type=charPoint;
    Typevalue.value="243class";
    LTNode *findnode=FindTreeNode(&tree,Typevalue,IsEqual);//查找判断是否是要找的数据
    if(findnode!=NULL)//查找到就替换
    {
        char *className="247class";
        findnode->data.value=className;
    }
    Travel(&tree,Print);
    
    Typevalue.type=structPoint;
    Typevalue.value=CreateStudent(1,"",0);//()查找数据类型位置
    findnode=FindTreeNode(&tree,Typevalue,IsEqual);
    if(findnode!=NULL)
    {
        struct student *stu=(struct student*)findnode->data.value;
        stu->score=60;
    }
    free(Typevalue.value);//结束要记得释放
    Travel(&tree,Print);


    // printf("height : %d\n",GetTreeHeight(&tree));

    // LTNode *classNode=FindTreeNode(&tree,"247class");
    // if(classNode!=NULL)
    // {
    //     LTNode *newNode=CreateTreeNode("zhangqiantao");
    //     ConnectBranch(classNode,newNode);
    // }
    // Travel(&tree);
    return 0;
}